from django.shortcuts import render, redirect
from django.http import JsonResponse

from web.models import User

def template(request):
    return render(request, "后台管理/template.html")

def index(request):
    return render(request, "后台管理/index.html")




# 活动相关
def activity(request):
    user = User.objects.all().first()
    return render(request, "后台管理/活动相关.html", {"user": user})

# 参与活动
def participate(request):
    return render(request, "后台管理/参与活动.html")

# 审批活动
def approve(request):
    return render(request, "后台管理/审批活动.html")

# 版本管理
def version(request):
    return render(request, "后台管理/版本管理.html")



# 登录
def login(request):
    if request.method == "POST":
        username = request.POST.get("username")
        password = request.POST.get("password")
        user = User.objects.filter(username=username, password=password, is_admin=True).first()
        if user:
            request.session['user'] = {
                "username": user.username,
                'user_id': user.user_id,
                "password": user.password,
                "is_admin": user.is_admin,
            }
            # 设置session有效期是10分钟
            request.session.set_expiry(60 * 10)
            return redirect("/web/ht/index/")
            
        else:
            return render(request, "后台管理/登录.html", {"error": "用户名或密码错误"})
    else:
        return render(request, "后台管理/登录.html")


# 财务
def financial(request):
    return render(request, "后台管理/财务.html")

